/**
 * This file contains javascript to handle information of medical agent.
 */
//////////////////////// LOADING A PAGE ////////////
function loadPage(sourceDiv, actionURL, serviceId) {        
    $.ajax ({       
        url:        actionURL + "?set_ajax_view", 
        data:		"service_id=" + serviceId,	             
        dataType:   "html",
        beforeSend: function() {                    
            $('div#listContent').hide(); 
            $('div#pageContent').hide("slow");                
        },
        complete:   function() {
            $('div#' + sourceDiv).show("fast");         
        },
        success:  function(data) {
            $("div#" + sourceDiv).html(data);          
        }
    });
}

function Edit(id) {
    $('div#listContent').hide();
    loadPage('pageContent', 'Edit', id);
}
 
function Save() {
    if (!isNotEmpty($('#name'), _("Empty name"), "status_message")) {
        return;
    }
    
    var submitData = $('#add_form').formSerialize();
        
    $.ajax ({
            type:   "POST",
            url:    "SaveAjax?set_ajax_view",
            data:   submitData,
            beforeSend: function() {
                $('#btn_save').hide();
                $('#barLoading').show();                
            },
            complete:   function() {
                $('#btn_save').show();
                $('#barLoading').hide();
            },
            success:    function(data) {
                $('div#pageContent').hide('slow');
                loadPage('listContent', 'List');
                $('div#listContent').show();
            }
        });
    
}

function Delete(id) {
	promptConfirm(_("Delete"),_('Are you sure to delete this clinical test?'), function() {
		submitDelete(id);
		return true;
	},'YesNo','warn',_('No'));
} 

function submitDelete(id) {
	$.ajax ({
		type: 	"GET",
		url:	"deleteAjax?set_ajax_view",
		data:	"id=" + id,
		success: function(data) {
			$('table#list tr#' + id).remove();
			
			if ($('table#list tbody tr').length == 0) {
				$('table#list tbody').append("<tr id='noresult'><td colspan='5'>" + _("None of a clinical test") + "</td></tr>");
			}
			
			$('table#list tr:even').addClass('alt');
			$('table#list tr:odd').removeClass('alt');			
		}
	});
}
